﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class SettlementTransferAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                

                //给日期输入框添加只读属性，不能在属性里直接设置，不然会取不到值
                this.txtDateReckoning.Attributes["readonly"] = "true";
                this.txtDateReckoning.Date = System.DateTime.Now;
                //设置经办人
              Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                this.lblManagerTrueName.Text = currentUser.TrueName;
                this.lblManagerUserName.Text = currentUser.UserName;
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Where(j => j.ClassID == (int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                this.BindAccountIDTo();
                this.BindAccountIDFrom();
                this.ShowCurrentBalance(this.ddlAccountIDFrom, this.lblCurrentBalanceFrom);
                this.ShowCurrentBalance(this.ddlAccountIDTo, this.lblCurrentBalanceTo);
            }
        }
        private void BindAccountIDTo()
        {
           // UART.BLL.Sales.Account bll = new UART.BLL.Sales.Account();
            var ds =mk.l.Sales_Account.Where(y=>y.RootDepartmentID.ToString()==this.lblRootDepartmentID.Text).ToList();
            this.ddlAccountIDTo.DataSource = ds;
            this.ddlAccountIDTo.DataTextField = "Name";
            this.ddlAccountIDTo.DataValueField = "AccountID";
            this.ddlAccountIDTo.DataBind();
        }

        /// <summary>
        /// Binds the account ID from.绑定账户列表
        /// </summary>
        private void BindAccountIDFrom()
        {
          //  Sales_Account bll = new Sales_Account();
            var ds = mk.l.Sales_Account.Where(y => y.RootDepartmentID.ToString() == this.lblRootDepartmentID.Text).ToList();
            this.ddlAccountIDFrom.DataSource = ds;
            this.ddlAccountIDFrom.DataTextField = "Name";
            this.ddlAccountIDFrom.DataValueField = "AccountID";
            this.ddlAccountIDFrom.DataBind();
        }

        /// <summary>
        /// Handles the Click event of the btnAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
               // UART.BLL.Sales.SettlementDetail bll = new UART.BLL.Sales.SettlementDetail();
               Sales_SettlementDetail mFrom = new Sales_SettlementDetail();
                   Sales_SettlementDetail mTo =  new Sales_SettlementDetail();
                //先添加转出账户信息
                mFrom.AccountID = int.Parse(this.ddlAccountIDFrom.SelectedValue);
                mFrom.DateReckoning = Convert.ToDateTime(this.txtDateReckoning.Date);
                mFrom.Flag = "1";
                mFrom.InfoFrom = "转账支出";
                mFrom.InfoTo = "转账收入";
                mFrom.Manager = this.lblManagerUserName.Text;
                mFrom.ModifiedDate = System.DateTime.Now;
                mFrom.Notes = mk.InputText(this.txtNotes.Text, 200);
                mFrom.PurchaseOrderID = 0;
                mFrom.RootDepartmentID = int.Parse(this.lblRootDepartmentID.Text);
                mFrom.SalesOrderID = 0;
                mFrom.SettlementAmount = -decimal.Parse(this.txtSettlementAmount.Text);
                //转账支出类型ID为3
                mFrom.TypeID = 3;
                //添加转入账户信息
                mTo.AccountID = int.Parse(this.ddlAccountIDTo.SelectedValue);
                mTo.DateReckoning = Convert.ToDateTime(this.txtDateReckoning.Date);
                mTo.Flag = "2";
                mTo.InfoFrom = "转账支出";
                mTo.InfoTo = "转账收入";
                mTo.Manager = this.lblManagerUserName.Text;
                mTo.ModifiedDate = System.DateTime.Now;
                mTo.Notes = mk.InputText(this.txtNotes.Text, 200);
                mTo.PurchaseOrderID = 0;
                mTo.RootDepartmentID = int.Parse(this.lblRootDepartmentID.Text);
                mTo.SalesOrderID = 0;
                mTo.SettlementAmount = decimal.Parse(this.txtSettlementAmount.Text);
                mTo.TypeID = 4;
                try
                {
                 //   bll.Add(mFrom);
                 //   bll.Add(mTo);
                    mk.l.Sales_SettlementDetail.InsertOnSubmit(mFrom);
                    mk.l.Sales_SettlementDetail.InsertOnSubmit(mTo);
                    mk.l.SubmitChanges();
                    decimal decSettlementAmount = decimal.Parse(this.txtSettlementAmount.Text);
                    //更新账户的信息
                   var bllAccount = mk.l.Sales_Account.ToList();
                 Sales_Account mAccountFrom = bllAccount.Where(h=>h.AccountID==int.Parse(this.ddlAccountIDFrom.SelectedValue)).FirstOrDefault();
                    mAccountFrom.Balance -= decSettlementAmount;
                 //   bllAccount.Update(mAccountFrom);
                              mk.l.SubmitChanges();
                    Sales_Account mAccountTo = bllAccount.Where(h=>h.AccountID==int.Parse(this.ddlAccountIDTo.SelectedValue)).FirstOrDefault()  ;
                    mAccountTo.Balance += decSettlementAmount;
                  //  bllAccount.Update(mAccountTo);
                    mk.l.SubmitChanges();
                    //提交事务
                 //   ContextUtil.SetComplete();
                   mk.Alert("转账成功！", Request.Url.ToString());
                }
                catch (Exception ex)
                {
                    //撤销事务
                 //   ContextUtil.SetAbort();
                   mk.Alert("转账失败！错误信息：" + ex.Message, Request.Url.ToString());
                }
            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the ddlAccountIDFrom control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ddlAccountIDFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ShowCurrentBalance(this.ddlAccountIDFrom, this.lblCurrentBalanceFrom);
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the ddlAccountIDTo control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ddlAccountIDTo_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ShowCurrentBalance(this.ddlAccountIDTo, this.lblCurrentBalanceTo);
        }

        /// <summary>
        /// Shows the current balance.
        /// </summary>
        /// <param name="ddlAccountID">The DDL account ID.</param>
        /// <param name="lblCurrentBalance">The LBL current balance.</param>
        private void ShowCurrentBalance(DropDownList ddlAccountID, Label lblCurrentBalance)
        {
            if (ddlAccountID.SelectedValue != "")
            {
              //  UART.BLL.Sales.Account bll = new UART.BLL.Sales.Account();
              Sales_Account m =mk.l.Sales_Account.Where(h=>h.AccountID==int.Parse(ddlAccountID.SelectedValue)).FirstOrDefault();
                lblCurrentBalance.Text = m.Balance.ToString();
            }
        }
    }
}
